<?php

class MenuManage
{
    const STATUS_NORMAL = 1;
    private $menu = array();

    public function init()
    {

    }

    private function createFullUrl($url)
    {
        $fullUrl = '';
        if (preg_match('/^(http|https):\/\//', $url)) {
            $fullUrl = $url;
        } elseif (preg_match('/^\//', $url)) {
            $fullUrl = 'http://' . $_SERVER['HTTP_HOST'] . $url;
        } else {
            $fullUrl = Yii::app()->createUrl($url);
        }
        return $fullUrl;
    }

    private function getUrl()
    {
        $url = "";
        if (isset(Yii::app()->controller->module)) {
            $module = Yii::app()->controller->module->id;
            $url .= $module;
        }
        $controller = Yii::app()->getController()->id;
        if ($url != "") $url .= "/";
        $url .= $controller;
        $action = Yii::app()->getController()->getAction()->id;
        if ($url != "") $url .= "/";
        $url .= $action;

        return $url;
    }

    public function isActive($fun_url)
    {
        $url = $this->getUrl();
        if ((strpos($url, $fun_url) === 0) && ((strlen($url) == strlen($fun_url)) || substr($url, strlen($fun_url), 1) == '/')) {
            return true;
        }
        return false;
    }

    public function getMenu($type = Menu::TYPE_BANNER)
    {
        switch ($type) {
            case Menu::TYPE_BANNER:
                $menus = array();
                $bannerMenus = Menu::getMenus($type);
                foreach ($bannerMenus as $parMenu) {
                    if ($parMenu->pid == 0) {
                        $parUrl = $this->createFullUrl($parMenu->url);
                        $subMenus = array();
                        $subActive = false;
                        foreach ($bannerMenus as $subMenu) {
                            if ($subMenu->pid == $parMenu->id) {
                                $subUrl = $this->createFullUrl($subMenu->url);
                                $subMenus[] = array(
                                    'label' => '<i class="fa ' . (($subMenu->icon != '') ? $subMenu->icon : 'fa-reorder') . '"></i>' . $subMenu->name,
                                    'url' => $subUrl,
                                    'visible' => true,
                                    'active' => $this->isActive($subMenu->url),
                                );
                                if ($this->isActive($subMenu->url)) {
                                    $subActive = true;
                                }
                            }
                        }
                        $active = $subActive || $this->isActive($parMenu->url);
                        $parMenuArray = array(
                            'label' => '<i class="fa ' . (($parMenu->icon != '') ? $parMenu->icon : 'fa-reorder') . '"></i>' . $parMenu->name,
                            'url' => $parUrl,
                            'visible' => true,
                            'active' => $active,
                            'linkOptions' => array('target' => $parMenu->target, 'class' => $parMenu->css_class),
                        );
                        if (!empty($subMenus)) {
                            $parMenuArray['items'] = $subMenus;
                        }
                        $menus[] = $parMenuArray;
                    }
                }
                return $menus;
                break;
            case Menu::TYPE_TOP:
                $menus = array();
                $topMenus = Menu::getMenus($type);
                foreach ($topMenus as $topMenu) {
                    $topUrl = $this->createFullUrl($topMenu->url);
                    $menus[] = array(
                        'label' => $topMenu->name,
                        'url' => $topUrl,
                        'visible' => true,
                        'active' => $this->isActive($topMenu->url),
                        'linkOptions' => array('target' => $topMenu->target, 'class' => $topMenu->css_class),
                    );
                }
                return $menus;
                break;
        }
    }
}

?>